---
title: "R_SM_A1"
author: "Loreto Cox & Carmen Le Foulon"
date: "2024-01-26"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(readxl)  
library(tidyverse)
library(lubridate)
```

# Info

Replication: Figures SM1 & SM2
# Data

## Electoral system and turnout data

Data downloaded from IDEA International

### Invalid voting

Downloaed on January 2024 from:
https://www.idea.int/data-tools/data/voter-turnout-database

Select "Export data" and download into folder "turnout" inside replication folder.

```{r}
inv <-read_excel("data/turnout/idea_export_voter_turnout_database_region.xlsx")
colnames(inv) <- c("pais", "iso2", "iso3", "tipo_elec", "date", "turnout", "totvote", "regist", "vap_turnout", "vap", "pop", "invalid", "compulsory")

inv <- na.omit(inv)
inv <- inv %>% mutate(date=as_date(date), year=year(date)) %>% select(-date) %>%
              select(pais, iso2, iso3,  year, everything())

nom2 <- c("turnout", "totvote", "regist", "vap_turnout", "vap", "pop", "invalid", "compulsory")

pres_e <- inv %>% filter(tipo_elec=="Presidential") %>% select(-tipo_elec)
colnames(pres_e)<- c("pais", "iso2", "iso3", "year", paste(nom2, "pres", sep="_"))
leg_e <- inv %>% filter(tipo_elec=="Parliamentary") %>% select(-tipo_elec)
colnames(leg_e)<- c("pais", "iso2", "iso3", "year", paste(nom2, "leg", sep="_"))

invalid<-full_join(pres_e, leg_e) %>% mutate(apres=ifelse(is.na(compulsory_pres)==T, 0, 1), 
                                         aleg=ifelse(is.na(compulsory_leg)==T, 0, 1),
                                         conc=ifelse(apres+aleg==2, 1, 0)) %>%
                               select(-apres, -aleg) %>%  
                             arrange(pais, year) %>% filter(is.na(invalid_leg)==F) %>% filter(year>2009)


```

### Electoral system

Downloaded on January 2024 from:
https://www.idea.int/data-tools/data/electoral-system-design-database

Select "Export data" and download into folder "turnout" inside replication folder.

```{r}
sist <-read_excel("data/turnout/idea_export_electoral_system_design_database.xlsx")

nsis <- c("pais", "iso2", "iso3", "year", "elecsis_family", "elecsis_nacleg", "ntiers", "leg_size_elec", "leg_size_vot", "pres_elec")

colnames(sist)<-nsis
sist <- sist %>% filter(year>2009)
sist$sist<-1
invalid$inv<-1

total <-full_join(sist, invalid)
total <- total %>% filter(is.na(sist)==F & is.na(inv)==F)
total <- total  %>% 
          mutate(sg=ifelse(pres_elec=="Not applicable", "Parliamentary", "Non_parliam"), 
                 inv_leg=as.numeric(gsub("%", "", invalid_leg)), inv_leg=inv_leg/100,
                sg_conc=ifelse(pres_elec=="Not applicable", "Parliamentary",
                                              ifelse(conc==0, "Non concurrent with presidential election ", 
                                                     "Concurrent with presidential election")))

tot_res <- total  %>% filter(elecsis_nacleg!="STV")


```

### VDEM


Downloaded on January 2024 from:
https://www.v-dem.net/data/the-v-dem-dataset/

Download into folder "turnout" inside replication folder.

```{r}

vdem <-  readRDS("data/turnout/V-Dem-CY-FullOthers_R_v13/V-Dem-CY-Full+Others-v13.rds")

dem<- vdem %>% filter(year>2009) %>% 
                select(iso3=country_text_id, year, v2x_polyarchy, v2x_libdem, v2x_partipdem) %>%
                mutate(polyarchy=cut(v2x_polyarchy, breaks=c(0, .33, 1), right=TRUE, labels=FALSE),
                       idem=1)


                       
                
```

### Data for figures


```{r}

fin <- full_join(dem, tot_res) %>% filter(is.na(inv)==F & is.na(idem)==F)  %>% filter(polyarchy>1) %>% 
        add_count(compulsory_leg, sg_conc, elecsis_family) %>% filter(n>5) 

gfilter_comp <- fin %>% filter(compulsory_leg!="No") 
gfilter_vol <- fin %>% filter(compulsory_leg=="No")

```

# Figures

## Figure SM1
```{r}


gsm1 <- ggplot(gfilter_vol) + geom_boxplot(aes(as_factor(elecsis_family), inv_leg, colour=sg_conc)) +
   labs(x="Electoral system family", y="Invalid voting") + scale_y_continuous(expand=c(0,0), limits = c(0,.3)) + theme_bw() +
   theme(legend.title = element_blank(), legend.position="bottom") 
 

 ggsave( "figures/fig_sm1.pdf", gsm1, width = 9, height=8)

   
```

## Figure  SM2

```{r}

gsm2 <- ggplot(gfilter_comp) + geom_boxplot(aes(as_factor(elecsis_family), inv_leg, colour=sg_conc)) +
   labs(x="Electoral system family", y="Invalid voting") + scale_y_continuous(expand=c(0,0), limits = c(0,.5)) + theme_bw() +
   theme(legend.title = element_blank(), legend.position="bottom") 

ggsave( "figures/fig_sm2.pdf", gsm2, width = 7, height=8)

```